/**
 * 
 */
package com.zufangbao.earth.service.impl;

import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.web.resolver.Page;
import com.zufangbao.earth.entity.contractpartical.ContractPartical;
import com.zufangbao.earth.service.ContractParticalService;
import com.zufangbao.earth.service.ParticalService;
import com.zufangbao.gluon.api.earth.TransferStatus;
import com.zufangbao.gluon.exception.CommonException;
import com.zufangbao.gluon.spec.earth.GlobalSpec4Earth.ErrorCode4ContractPartical;
import com.zufangbao.gluon.spec.earth.GlobalSpec4Earth.ErrorMsg4ContractPartical;
import com.zufangbao.gluon.spec.global.GlobalCodeSpec.GeneralErrorCode;
import com.zufangbao.gluon.spec.global.GlobalMsgSpec.GeneralErrorMsg;
import com.zufangbao.sun.entity.company.corp.App;
import com.zufangbao.sun.entity.contract.Contract;
import com.zufangbao.sun.entity.order.Order;
import com.zufangbao.sun.service.AppService;
import com.zufangbao.sun.service.ContractService;
import com.zufangbao.sun.service.OrderService;

/**
 * @author wukai
 *
 */
@Service("contractParticalService")
public class ContractParticalServiceImpl extends GenericServiceImpl<ContractPartical> implements ContractParticalService {

	@Autowired
	private ContractService contractService;
	
	@Autowired
	private AppService appService;
	
	@Autowired
	private ParticalService particalService;
	
	@Autowired
	private OrderService orderService;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ContractPartical> listAssignContracByUniqueIdAndAppId(
			String particalUniqueId, String appId, Page page) {
		
		if(StringUtils.isEmpty(particalUniqueId) || StringUtils.isEmpty(appId)){
			
			return Collections.EMPTY_LIST;
		} 
		if(null == appService.getApp(appId)){
			
			return Collections.EMPTY_LIST;
		}
		if(null == particalService.getParticalByUniqueId(particalUniqueId)){
			
			return Collections.EMPTY_LIST;
		}
		App app = appService.getApp(appId);
		
		Filter filter = new Filter();
		
		filter.addEquals("particalUniqueId",particalUniqueId);
		
		filter.addEquals("contract.app", app);
		
		filter.addEquals("isDelete", false);
		
		filter.addEquals("channelAssgin", TransferStatus.INCLUDED);
		
		return list(ContractPartical.class, filter, page);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Contract> listNoAssignContract(String appId, Page page) {
		
		if(StringUtils.isEmpty(appId)){
			
			return (List<Contract>) Collections.EMPTY_LIST;
		}
		App app = appService.getApp(appId);
		
		if(app == null){
			
			return (List<Contract>) Collections.EMPTY_LIST;
		}
		
		Filter filter = new Filter();
		
		filter.addLessThan("transferStatus", TransferStatus.INCLUDED.ordinal());
		
		filter.addEquals("app", app);
		
		return contractService.list(Contract.class, filter, page);
		
	}

	@Override
	public void assignContract(String particalUniqueId, Long contractId)
			throws CommonException {
		
		if(StringUtils.isEmpty(particalUniqueId)){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		if(contractId <= 0L){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		Contract contract = contractService.getContract(contractId);
		
		if(null == contract){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		
		Filter filter = new Filter();
		
		filter.addEquals("contract", contract);
		
		filter.addEquals("particalUniqueId", particalUniqueId);
		
		filter.addEquals("channelAssgin", TransferStatus.INCLUDED);
		
		filter.addEquals("isDelete", false);
		
		if(CollectionUtils.isNotEmpty(list(ContractPartical.class, filter))){
			
			throw new CommonException(ErrorCode4ContractPartical.ERROR_EXIST_CONTRACT_PARTICAL, ErrorMsg4ContractPartical.MSG_EXIST_CONTRACT_PARTICAL);
		}
		
		ContractPartical contractPartical = new ContractPartical(particalUniqueId,contract,TransferStatus.INCLUDED);
		
		genericDaoSupport.save(contractPartical);
		
		contract.setTransferStatus(TransferStatus.INCLUDED.ordinal());
		
		contractService.saveOrUpdate(contract);
		
		List<Order> orderList = orderService.getOrderList(contract);
		
		for(Order order : orderList){
			
			order.setTransferStatus(TransferStatus.INCLUDED.ordinal());
			
			order.setUniqueParticleId(particalUniqueId);
			
			orderService.saveOrUpdate(order);
		}
		
	}

	@Override
	public void revokeContract(String particalUniqueId, Long contractId) throws CommonException {
		
		if(StringUtils.isEmpty(particalUniqueId)){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		if(contractId <= 0L){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		Contract contract = contractService.getContract(contractId);
		
		if(null == contract){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		
		Filter filter = new Filter();
		
		filter.addEquals("contract", contract);
		
		filter.addEquals("particalUniqueId", particalUniqueId);
		
		filter.addEquals("channelAssgin", TransferStatus.INCLUDED);
		
		filter.addEquals("isDelete", false);
		
		List<ContractPartical> contractParticals = this.list(ContractPartical.class, filter);
		
		if(CollectionUtils.isNotEmpty(contractParticals)){
			
			ContractPartical oldContractPartical = contractParticals.get(0);
			
			oldContractPartical.setDelete(true);
			
			genericDaoSupport.saveOrUpdate(oldContractPartical);
			
			ContractPartical contractPartical = new ContractPartical(particalUniqueId,contract,TransferStatus.DRAW);
			
			genericDaoSupport.save(contractPartical);
			
			contract.setTransferStatus(TransferStatus.DRAW.ordinal());
			
			contractService.saveOrUpdate(contract);
			
			List<Order> orderList = orderService.getOrderList(contract);
			
			for(Order order : orderList){
				
				order.setTransferStatus(TransferStatus.DRAW.ordinal());
				
				orderService.saveOrUpdate(order);
			}
		}
	}

	@Override
	public void assignContracts(String uniqueId, List<Long> contractIdList)
			throws CommonException {
		
		if(StringUtils.isEmpty(uniqueId)){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}

		if(CollectionUtils.isEmpty(contractIdList)){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		for(Long contractId : contractIdList){
			
			assignContract(uniqueId, contractId);
		}
		
	}

	@Override
	public void revokeContracts(String uniqueId, List<Long> contractIdList)
			throws CommonException {
		
		if(StringUtils.isEmpty(uniqueId)){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}

		if(CollectionUtils.isEmpty(contractIdList)){
			
			throw new CommonException(GeneralErrorCode.ERROR_ILLEGAL_PARAM, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		for(Long contractId : contractIdList){
			
			revokeContract(uniqueId, contractId);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContractPartical> listAssignContracByUniqueIdAndAppId(
			String particalUniqueId, String appId) {
		
		if(StringUtils.isEmpty(particalUniqueId) || StringUtils.isEmpty(appId)){
			
			return Collections.EMPTY_LIST;
		} 
		if(null == appService.getApp(appId)){
			
			return Collections.EMPTY_LIST;
		}
		if(null == particalService.getParticalByUniqueId(particalUniqueId)){
			
			return Collections.EMPTY_LIST;
		}
		App app = appService.getApp(appId);
		
		Filter filter = new Filter();
		
		filter.addEquals("particalUniqueId",particalUniqueId);
		
		filter.addEquals("contract.app", app);
		
		filter.addEquals("isDelete", false);
		
		filter.addEquals("channelAssgin", TransferStatus.INCLUDED);
		
		return list(ContractPartical.class, filter);
	}

}
